package com.itheima.spring.bestpractive.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.spring.bestpractive.common.exception.BizEnum;
import com.itheima.spring.bestpractive.common.exception.BizException;
import com.itheima.spring.bestpractive.mapping.AssessmentMapping;
import com.itheima.spring.bestpractive.pojo.*;
import com.itheima.spring.bestpractive.service.AssessmentService;
import com.itheima.spring.bestpractive.utils.AssessmentCalculate;
import com.itheima.spring.bestpractive.vo.req.DisputesReqVo;
import com.itheima.spring.bestpractive.vo.resp.employee.EmpEvaRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.spring.bestpractive.vo.resp.assessment.AssessmentIndicatorRespVo;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @author: HZY
 * @date: 2025/6/25 17:10
 * @Content:
 */
@Service
@Transactional
public class AssessmentServiceImpl implements AssessmentService {
    @Autowired
    private AssessmentMapping assessmentMapping;

    @Autowired
    private AssessmentCalculate assessmentCalculate;



    @Override
    public List<AssessmentPlan> getAssessmentPlanList(long empId) {
        System.out.println(assessmentMapping.selectAssessmentPlanList(empId));
        return assessmentMapping.selectAssessmentPlanList(empId);
    }

    @Override
    public List<AssessmentIndicator> getAssessmentIndicatorList(AssessmentPlan plan) {
        List<AssessmentIndicator> assessmentIndicatorList = assessmentMapping.selectAssessmentIndicatorList(plan);
        if(assessmentMapping == null){
            throw new BizException(BizEnum.DATA_NOT_FOUND);
        }
        return assessmentIndicatorList;

    }

    @Override
    public void saveScore(List<EmployeeIndicatorScore> scores) {
        AssessmentResult result = new AssessmentResult();
        BigDecimal bigDecimal = assessmentCalculate.calculateScore(scores);
        result.setScore(bigDecimal);
        result.setEmployeeId(scores.get(0).getEmployeeId());
        result.setPlanId(scores.get(0).getPlanId());
        result.setStatus(0);
        System.out.println(result);
        assessmentMapping.insertAssessmentResult(result);
        assessmentMapping.insertScore(scores);
    }

    @Override
    public AssessmentResult getAssessmentResult(long empId,long planId) {
        return assessmentMapping.selectAssessmentResult(empId,planId);
    }

    @Override
    @Transactional
    public void publishAssessment(AssessmentPlan plan, List<AssessmentIndicator> indicator) {
        Date now = new Date();
        if(now.before(plan.getStartDate())){
            plan.setStatus(-1);
        }else {
            plan.setStatus(1);
        }

        assessmentMapping.insertAssessmentPlan(plan);
        assessmentMapping.insertAssessmentIndicator(indicator);
        List<Long> indicatorId = indicator.stream().map(AssessmentIndicator::getIndicatorId).toList();
        assessmentMapping.insertAssessmentIndicatorPlan(plan.getPlanId(), indicatorId);
    }


    @Override
    @Transactional
    public void deleteAssessmentPlan(List<Long> planIds) {
        List<Long> longs = assessmentMapping.selectIndicatorByPlan(planIds);
        if(longs.isEmpty()){
            throw new BizException(BizEnum.DATA_NOT_FOUND);
        }
        assessmentMapping.deleteAssessmentPlanAndIndicator(planIds);
        assessmentMapping.deleteIndicator(longs);
    }

    @Override
    public List<AssessmentIndicatorRespVo> getIndicatorByPId(long planId, long empId) {
        return assessmentMapping.selectIndicatorByEmp(planId, empId);
    }

    @Override
    public List<EmployeeIndicatorScore> getIndicatorScore(List<Long> indicatorIds, long empId) {
        return assessmentMapping.selectIndicatorScore(indicatorIds,empId);
    }

    @Override
    public List<AssessmentPlan> getPlanByName(String planName) {
        return assessmentMapping.selectPlanByName(planName);
    }

    @Override
    public List<AssessmentPlan> getAllPlan() {
        return assessmentMapping.selectAllPlan();
    }

    @Override
    public PageInfo<AssessmentPlan> getPlan(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<AssessmentPlan> allPlan = assessmentMapping.selectAllPlan();
        PageInfo<AssessmentPlan> pageInfo = new PageInfo<>(allPlan);
        return pageInfo;
    }

    @Override
    public List<Employee> getEvaluatorByPlanId(long planId) {
        return assessmentMapping.selectPlanEmp(planId);
    }

    @Override
    public void updateResultStatus(Map<String, Object> data) {
        System.out.println(data);
        assessmentMapping.updateResultStatus(((Number)data.get("employeeId")).longValue(), ((Number)data.get("planId")).longValue(), ((Number)data.get("status")).intValue());
        assessmentMapping.updateResultStatus2(((Number)data.get("employeeId")).longValue(), ((Number)data.get("planId")).longValue(), ((Number)data.get("status")).intValue());
    }

    @Override
    public void saveDisputes(Map<String, Object> disputes) {
        assessmentMapping.saveDisputes(((Number)disputes.get("employeeId")).longValue(),((Number)disputes.get("planId")).longValue(),(String) disputes.get("objectionContent"));
    }

    @Override
    public List<AssessmentPlan> getDisputes() {
        return assessmentMapping.getDisputes();
    }

    @Override
    public List<Employee> getDisputesEmp(long planId) {
        return assessmentMapping.getDisputesEmpByPlan(planId);
    }

    @Override
    public List<Disputes> getDisputesInfo(long planId, long employeeId) {
        return  assessmentMapping.getDisputesByEmpPlan(planId,employeeId);
    }

    @Override
    public void updataDisputesByEmpPlan(DisputesReqVo disputesReqVo) {
        List<EmployeeIndicatorScore> indicatorScores = new ArrayList<>();
        for (DisputesReqVo.DisputeItem item : disputesReqVo.getItems()) {
            EmployeeIndicatorScore employeeIndicatorScore = new EmployeeIndicatorScore();
            employeeIndicatorScore.setIndicatorId(item.getIndicatorId());
            employeeIndicatorScore.setScore(item.getScore());
            indicatorScores.add(employeeIndicatorScore);
            assessmentMapping.updataIndicatorByEmpPlan(item.getIndicatorId(),
                    disputesReqVo.getEmployeeId(),
                    item.getScore(),
                    item.getComment());

        }
        BigDecimal ReScore = assessmentCalculate.calculateScore(indicatorScores);
        assessmentMapping.updataResultByEmpPlan(disputesReqVo.getPlanId(),disputesReqVo.getEmployeeId(),ReScore);
        assessmentMapping.updataDisputesStatus(disputesReqVo.getPlanId(),disputesReqVo.getEmployeeId());
    }


}
